/*
* @(#)ListEmailArchiveRuleAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.dms.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.rules.RuleOperationConstant;
import com.dcivision.rules.bean.RuleBaseObject;
import com.dcivision.rules.dao.RuleBaseObjectDAObject;


/**
 * <p>Class Name:       ListEmailArchiveRuleAction    </p>
 * <p>Description:      The list action class for ListEmailArchiveRule.jsp</p>
 * 
 * @author          Tony Chen
 * @company         DCIVision Limited
 * @creation date   28/08/2005
 * @version         $Revision: 1.6 $
*/
public class ListEmailArchiveRuleAction extends AbstractListAction{

	private String majorDAOClassName = "com.dcivision.dms.dao.EmailArichiveRuleDAObject";
	
	/**
   *  Constructor - Creates a new instance of ListDmsDocumentAction and define the default listName.
   */
  public ListEmailArchiveRuleAction(){
    super();
    this.setListName("publicRuleList");  
  }
  
  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_PUBLIC_RULE_SETUP);

  }
  
  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return(this.majorDAOClassName);
  }
  
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException { 	
  	
  	ListEmailArchiveRuleForm listForm = (ListEmailArchiveRuleForm)form;
  	
  	//List Private Rule 
  	if(RuleOperationConstant.PRIVATE_RULE_FUNCTION.equals(listForm.getRuleType())){
  		request.setAttribute(GlobalConstant.FUNCTION_CODE_KEY, null);
  		this.setFunctionCode(SystemFunctionConstant.DMS_PRIVATE_RULE_SETUP);
  		request.setAttribute(GlobalConstant.DAO_KEY, null);
  		this.majorDAOClassName = "com.dcivision.dms.dao.EmailArichivePrivateRuleDAObject";
  	
    //List Public Rule		
  	} else {
  		request.setAttribute(GlobalConstant.FUNCTION_CODE_KEY, null);
  		this.setFunctionCode(SystemFunctionConstant.DMS_PUBLIC_RULE_SETUP);
  		request.setAttribute(GlobalConstant.DAO_KEY, null);
  		this.majorDAOClassName = "com.dcivision.dms.dao.EmailArichiveRuleDAObject";
  	}
  	
    /***********************************************
     *   Enable/Disable List Data
     ***********************************************/
    if ("OP_MODE_ENABLE".equals(listForm.getOpMode()) || 
        "OP_MODE_DISABLE".equals(listForm.getOpMode()) ) {
      
      this.enableListData((AbstractSearchForm)form, request, "OP_MODE_ENABLE".equals(listForm.getOpMode()) );
      listForm.setOpMode(null);
      listForm.setNavMode(null);
    
    } else if ("OP_MODE_UP".equals(listForm.getOpMode()) || 
        "OP_MODE_DOWN".equals(listForm.getOpMode())) {
      
      this.moveUpListData((AbstractSearchForm)form, request, "OP_MODE_UP".equals(listForm.getOpMode()) );
      listForm.setOpMode(null);
      listForm.setNavMode(null);
    }
    log.debug("=========navMode:===="+listForm.getNavMode());
  	return super.execute( mapping,form, request,  response);
  }

  /**
   * enable/disable the selected record
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  private void enableListData(AbstractSearchForm form, HttpServletRequest request, boolean enable) throws ApplicationException {
    RuleBaseObjectDAObject dao = (RuleBaseObjectDAObject)this.getMajorDAObject(request);
    String[] ruleIDArr = form.getBasicSelectedID();
    
    for (int i = 0; i < ruleIDArr.length; i++) {
      RuleBaseObject rule = (RuleBaseObject)dao.getObjectByID(new Integer(ruleIDArr[i]) );
      if (enable) {
        rule.setRuleEnable(GlobalConstant.TRUE);
      } else {
        rule.setRuleEnable(GlobalConstant.FALSE);
      }
      dao.updateObject(rule);
    }
  }
  
  /**
   * move up/down the selected record 
   * @param form
   * @param request
   * @param moveUp
   * @throws ApplicationException
   */
  private void moveUpListData(AbstractSearchForm form, HttpServletRequest request, boolean moveUp) throws ApplicationException {
    RuleBaseObjectDAObject dao = (RuleBaseObjectDAObject)this.getMajorDAObject(request);
    String[] ruleIDArr = form.getBasicSelectedID();    
    
    // move up
    if (moveUp) {
    	for (int i = 0; i < ruleIDArr.length; i++) {
    		RuleBaseObject rule = (RuleBaseObject)dao.getObjectByID(new Integer(ruleIDArr[i]) );
    		if ( "000001".equals(rule.getRuleSeqNo()) ) {
    			continue;
    		}
    		RuleBaseObject previousRule = (RuleBaseObject)dao.getObjectByRuleSeqNo( String.valueOf(TextUtility.parseInteger(rule.getRuleSeqNo()) - 1) );
    		rule.setRuleSeqNo( String.valueOf(TextUtility.parseInteger(rule.getRuleSeqNo()) - 1) );
    		dao.updateObject(rule);
    		
    		previousRule.setRuleSeqNo( String.valueOf(TextUtility.parseInteger(previousRule.getRuleSeqNo()) + 1) );
    		dao.updateObject(previousRule);
    	}
   
    // move down
    } else {
    	for (int i = ruleIDArr.length - 1 ; i > -1; i--) {
    		RuleBaseObject rule = (RuleBaseObject)dao.getObjectByID(new Integer(ruleIDArr[i]) );
    		RuleBaseObject nextRule = (RuleBaseObject)dao.getObjectByRuleSeqNo( String.valueOf(TextUtility.parseInteger(rule.getRuleSeqNo()) + 1) );
    		if (Utility.isEmpty(nextRule)) {
    			continue;
    		}
    		
    		rule.setRuleSeqNo( String.valueOf(TextUtility.parseInteger(rule.getRuleSeqNo()) + 1) );
    		dao.updateObject(rule);
    		
    		nextRule.setRuleSeqNo( String.valueOf(TextUtility.parseInteger(nextRule.getRuleSeqNo()) - 1) );
    		dao.updateObject(nextRule);    		
    	}    	
    }
    
  }
  
}
